<?php

namespace App\Repositories\Eloquent;

use App\Models\PolicyUpload;
use App\Repositories\Contracts\TcPolicyInterface;
use App\Services\PolicyUpload\TcService;
use MakeRep\Repository;

class TcPolicyRepository extends Repository implements TcPolicyInterface
{

    public $view = 'tcpolicy';
    public $fillable = ['admin_id','policy_id','policy_code','airline','platform','flight_code','share_flight_code','depart_city_code','arrived_city_code','depart_at','cabin','price','seat','show','status','ext','upload_status','upload_msg'];

    function model()
    {
        return PolicyUpload::class;
    }

    public function create($param)
    {
        //平台上传
        switch ($param['platform']){

            case 40: {

                TcService::TcDataSet($param);

                //先保存数据等待回调
                $this->savePolicy($param,null);

                //处理数据
                $specialPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');

                $uploadRes = $specialPolicyInterface->addSpecial([$param]);

                if (!empty($uploadRes) && isset($uploadRes['success'])){
                    if ($uploadRes['success'] == true) {
                        return true;
                    } else if ($uploadRes['success'] == false) {
                        $errorMessage = $uploadRes['errorMessage'];
                        if($errorMessage) {
                            return $errorMessage;
                        }
                    }
                }

                return '网络有问题，稍后重试';

            }
        }
    }

    /**
     * 保存政策
     * @param $param
     * @param $uploadRes
     * @return void
     */
    private function savePolicy($param,$policyId){

        switch ($param['platform']){
            case 10:
            case 40: {

                $data = [
                    'admin_id'              => $param['admin']['id'],
//                    'policy_id'             => $policyId,
                    'policy_code'           => $param['policyCode'],
                    'airline'               => $param['airCompany'],
                    'platform'              => $param['platform'],
                    'flight_code'           => $param['flightNoList'],
                    'depart_city_code'      => $param['startPort'],
                    'arrived_city_code'     => $param['endPort'],
                    'depart_at'             => $param['startDate'],
                    'cabin'                 => $param['cabinCodeList'],
                    'price'                 => $param['fixedTotalPrice'],
                    'seat'                  => $param['everydayInventory'],
                    'status'                => $param['isValid'] == 1 ? 1 : 2,
                    'ext'                   => $param['ext'],
                    'upload_status'         => 1,
                ];
                break;
            }
        }

        if (empty($param['id'])) {
            $res = $this->add($data);
        } else {
            $res = $this->update(['id'=>$param['id']],$data);
        }
    }

    /**
     * 更新政策
     * @param $param
     * @return void
     */
    public function updatePolicy($param) {
        switch ($param['platform']){

            case 40: {

                TcService::TcDataSet($param);

                //先保存数据等待回调
                $this->savePolicy($param,null);

                //处理数据
                $specialPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');

                $uploadRes = $specialPolicyInterface->updatePolicyById([$param]);

                if (!empty($uploadRes) && isset($uploadRes['success'])){
                    if ($uploadRes['success'] == true) {
                        return true;
                    } else if ($uploadRes['success'] == false) {
                        $errorMessage = $uploadRes['errorMessage'];
                        if($errorMessage) {
                            return $errorMessage;
                        }
                    }
                }

                return '网络有问题，稍后重试';

            }
        }
    }


    /**
     * 删除政策
     * @param $param
     * @return void
     */
    public function delPolicy($param) {
        switch ($param['platform']){

            case 40: {

                if(empty($param['PolicyID'])) {
                    $where = ['id' => $param['id']];
                    $result = $this->delete($where);
                    return $result == 1 ? true : false;
                }

                TcService::TcDataSet($param);

                $where = ['policy_id' => $param['PolicyID']];
                $policy = $this->index($where);
                if ($policy) {

                    $param['ext'] = $policy['ext'];

                    //处理数据
                    $specialPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');
                    $uploadRes = $specialPolicyInterface->deleteSpecial($param['PolicyID'], $param['ext'] , $param['url'],1,1);

                    if (!empty($uploadRes) && isset($uploadRes['success'])){
                        if ($uploadRes['success'] == true) {
                            return true;
                        } else if ($uploadRes['success'] == false) {
                            $errorMessage = $uploadRes['errorMessage'];
                            if($errorMessage) {
                                return $errorMessage;
                            }
                        }
                    }
                }


                return '网络有问题，稍后重试';

            }
        }
    }

}